package com.sunny.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunny.dto.ArticleDTO;
import com.sunny.entity.Article;
import com.sunny.mapper.ArticleMapper;
import com.sunny.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author sunny
 * @since 2025-10-24
 */
@Service
@AllArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    private final ArticleMapper articleMapper;

    @Override
    public Integer add(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO, article);
        return articleMapper.insert(article);
    }

    @Override
    public Page<Article> blogs(Page<Article> articlePage) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>()
                .eq(Article::getIsDeleted, false);
        return articleMapper.selectPage(articlePage, queryWrapper);
    }

    @Override
    public Boolean deleteArticle(Long articleId) {
        return this.lambdaUpdate().eq(Article::getId, articleId)
                .set(Article::getIsDeleted, true)
                .update();
    }

    @Override
    public ArticleDTO getArticleById(Long articleId) {
        ArticleDTO articleDTO = new ArticleDTO();
        Article article = articleMapper.selectById(articleId);
        if(ObjectUtil.isNull(article)){
            return null;
        }
        BeanUtils.copyProperties(article, articleDTO);
        return articleDTO;
    }
}
